//
//  FitnessTableViewController.m
//  B
//
//  Created by lanou on 16/5/9.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "FitnessTableViewController.h"
#import "FitnessModel.h"
#import "FitnessTableViewCell.h"

@interface FitnessTableViewController ()

@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) NSArray *imageArrs;

@end

@implementation FitnessTableViewController

- (NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (NSArray *)imageArrs
{
    if (!_imageArrs) {
        _imageArrs = [NSArray array];
    }
    return _imageArrs;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self requestData];
    [self.tableView registerNib:[UINib nibWithNibName:@"FitnessTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (void)requestData
{
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    [manager POST:@"http://api.aishou.com/api/topic/topics?json=%7B%22start%22%3A%220%22%2C%22channelId%22%3A%220%22%2C%22type%22%3A%224%22%2C%22userId%22%3A%22907435%22%2C%22offset%22%3A%2220%22%7D" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        if (responseObject != nil) {
//            NSDictionary *respObj = responseObject;
//            NSArray *array = respObj[@"result"][@"list"];
//            for (NSDictionary *dic in array) {
//                FitnessModel *mode = [[FitnessModel alloc] init];
//                [mode setValuesForKeysWithDictionary:dic];
//                [self.listArray addObject:mode];
//            }
//            [self.tableView reloadData];
//        }
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"0");
//    }];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://a1.go2yd.com/Website/contents/content?platform=1&cv=3.2.2&offline_download=true&appid=slim&related_docs=true&bottom_channels=true&bottom_comments=true&docid=0DEmTa4n,0DFABYta,0DFAkITn,0DEaUEt4,0DEZtpD1,0DF6aaqM,0DEnaUTl,0DEepnpe&version=010912&net=wifi" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *respObj = responseObject;
//        NSLog(@"%@", respObj);      
        for (NSDictionary *dic in respObj[@"documents"]) {
            FitnessModel *mode = [[FitnessModel alloc] init];
            [mode setValuesForKeysWithDictionary:dic];
            [self.listArray addObject:mode];
        }
        [self.tableView reloadData];
        NSString *string = @"苹果";
        NSLog(@"%@", [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@", error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FitnessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    // Configure the cell...
    FitnessModel *model = self.listArray[indexPath.row];
    cell.titleLable.text = model.title;
    cell.nicknameLable.text = model.nickname;
    if (model.viewCount.integerValue < 10000) {
        cell.viewCountLable.text = model.viewCount.stringValue;
    } else {
        cell.viewCountLable.text = [NSString stringWithFormat:@"%ld万+", (model.viewCount.integerValue / 10000)];
    }
    
    cell.commentCountLable.text = model.commentCount.stringValue;
    NSLog(@"%@", model.smallImgUrl);
//    self.imageArrs = model.smallImgUrl;
//    cell.imageVIEW1.image = [UIImage imageWithData:model.image];
//    NSData *str = model.image;
//    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
